package com.hsyt.pms.org.dubbo.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.hsyt.pms.api.constants.OrgConstants;
import com.hsyt.pms.api.dto.org.*;
import com.hsyt.pms.api.vo.org.OrgHelp;
import com.hsyt.pms.org.dubbo.OrgService;
import com.hsyt.pms.org.dubbo.OrgTypeService;
import com.hsyt.pms.orm.org.OrgDao;
import com.hsyt.pms.orm.org.OrgTypeDao;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @ClassName: OrgTypeServiceImpl
 * @Description:  组织类型管理实现
 * @author wangze@hsyuntai.com
 * @date 2019年2月12日 下午2:45:51
 */
@Slf4j
@Component
@Service(version = "0.0.1", interfaceClass = OrgTypeService.class, registry = "pms", group = "pmsOrg", timeout = 3000)
public class OrgTypeServiceImpl implements OrgTypeService {

    private static final Logger LOGGER = LoggerFactory.getLogger(OrgTypeServiceImpl.class);

    @Resource
    private OrgDao orgDao;

    @Resource
    private OrgTypeDao orgTypeDao;

    public int updateByPrimaryKey(OrgDto orgDto) {
        int result = orgDao.updateByPrimaryKey(orgDto);

        if (orgDto instanceof OrgFireDto) {
            result = orgTypeDao.updateOrgFire((OrgFireDto) orgDto);
        } else if (orgDto instanceof OrgSocialDto) {
            result = orgTypeDao.updateOrgSocial((OrgSocialDto) orgDto);
        } else if (orgDto instanceof OrgIndustryDto) {
            result = orgTypeDao.updateOrgIndustry((OrgIndustryDto) orgDto);
        } else if (orgDto instanceof OrgBusinessDto) {
            result = orgTypeDao.updateOrgBusiness((OrgBusinessDto) orgDto);
        } else if (orgDto instanceof OrgTenementDto) {
            result = orgTypeDao.updateOrgTenement((OrgTenementDto) orgDto);
        } else if (orgDto instanceof OrgAcceptCenterDto) {
            result = orgTypeDao.updateOrgAcceptCenter((OrgAcceptCenterDto) orgDto);
        } else if (orgDto instanceof OrgMaintenanceDto) {
            result = orgTypeDao.updateOrgMaintenance((OrgMaintenanceDto) orgDto);
        } else if (orgDto instanceof OrgMicroFireDto) {
            result = orgTypeDao.updateOrgMicroFire((OrgMicroFireDto) orgDto);
        }
        return result;
    }

    @Override
        public int insertOrg(OrgDto orgDto) {
        int result = orgDao.insertOrg(orgDto);
        if (orgDto instanceof OrgFireDto) {
            result = orgTypeDao.insertOrgFire((OrgFireDto) orgDto);
        } else if (orgDto instanceof OrgSocialDto) {
            result = orgTypeDao.insertOrgSocial((OrgSocialDto) orgDto);
        } else if (orgDto instanceof OrgIndustryDto) {
            result = orgTypeDao.insertOrgIndustry((OrgIndustryDto) orgDto);
        } else if (orgDto instanceof OrgBusinessDto) {
            result = orgTypeDao.insertOrgBusiness((OrgBusinessDto) orgDto);
        } else if (orgDto instanceof OrgTenementDto) {
            result = orgTypeDao.insertOrgTenement((OrgTenementDto) orgDto);
        } else if (orgDto instanceof OrgAcceptCenterDto) {
            result = orgTypeDao.insertOrgAcceptCenter((OrgAcceptCenterDto) orgDto);
        } else if (orgDto instanceof OrgMaintenanceDto) {
            result = orgTypeDao.insertOrgMaintenance((OrgMaintenanceDto) orgDto);
        } else if (orgDto instanceof OrgMicroFireDto) {
            result = orgTypeDao.insertOrgMicroFire((OrgMicroFireDto) orgDto);
        }
        return result;
    }

    public OrgDto initOrg(Long parentOrgId, Integer orgType) {
        OrgDto result = new OrgDto();
        OrgDto pdto = null;
        if (parentOrgId != null && parentOrgId > 0) {
            pdto = orgDao.getOrgById(parentOrgId);
        }
        if (pdto == null) {
            OrgDto rootDto = orgDao.getRootOrg(orgType);
            String maxSysOrgCode = rootDto == null ? OrgConstants.SYS_ORG_CODE_INIT : rootDto.getSysOrgCode();
            result.setOrgLevel(1);
            result.setSysOrgCode(OrgHelp.nextSysOrgCode(maxSysOrgCode));
            result.setOrgCode(OrgHelp.nextOrgCode(maxSysOrgCode));
        } else {
            String maxSysOrgCode = orgDao.maxSysOrgCode(parentOrgId, pdto == null ? orgType : pdto.getOrgType());
            result.setParentOrgCode(pdto.getOrgCode());
            result.setOrgLevel(pdto.getOrgLevel() + 1);
            result.setSysOrgCode(OrgHelp.nextSysOrgCode(maxSysOrgCode));
            result.setOrgCode(OrgHelp.nextOrgCode(maxSysOrgCode));
        }
        return result;
    }

    public String maxSysOrgCode(Long orgId, Integer orgType) {
        return orgDao.maxSysOrgCode(orgId, orgType);
    }


}
